#include "StdAfx.h"
#include "SgBox.h"
#include "SgPolygon.h"
#include "SgTexture.h"
CSgBox::CSgBox(double x, double y, double z, CSgTexture *texture)
{
	m_x = x;
	m_y = y;
	m_z = z; 


	double box[8][4] = {{0, 0, -m_z/2, 1}, {0, 0, m_z/2, 1}, {m_x, 0, m_z/2, 1}, {m_x, 0, -m_z/2, 1},
		{0, m_y, -m_z/2, 1}, {0, m_y, m_z/2, 1}, {m_x, m_y, m_z/2, 1}, {m_x, m_y, -m_z/2, 1}};
	CGrVector boxNormals[6] = {CGrVector(1, 0, 0, 1), CGrVector(0, 1, 0, 1), CGrVector(0, 0, 1, 1),
		CGrVector(-1, 0, 0, 1), CGrVector(0, -1, 0, 1), CGrVector(0, 0, -1, 1)};
	


	m_boxBottom = new CSgPolygon();
	m_boxBottom->AddNormal(boxNormals[4]);
	m_boxBottom->AddVertex3dv(box[2]);
	//m_boxBottom->AddNormal(boxNormals[4]);
	m_boxBottom->AddVertex3dv(box[1]);
	//m_boxBottom->AddNormal(boxNormals[4]);
	m_boxBottom->AddVertex3dv(box[0]);
	//m_boxBottom->AddNormal(boxNormals[4]);
	m_boxBottom->AddVertex3dv(box[3]);
	
	m_boxBottom->AddTexCoord(CGrVector(z/3,1,1));
	m_boxBottom->AddTexCoord(CGrVector(0,1,1));
	m_boxBottom->AddTexCoord(CGrVector(0,0,1));
	m_boxBottom->AddTexCoord(CGrVector(z/3,0,1));

	m_boxLeft = new CSgPolygon();
	m_boxLeft->AddNormal(boxNormals[2]);
	m_boxLeft->AddVertex3dv(box[1]);
	//m_boxLeft->AddNormal(boxNormals[2]);
	m_boxLeft->AddVertex3dv(box[2]);
	//m_boxLeft->AddNormal(boxNormals[2]);
	m_boxLeft->AddVertex3dv(box[6]);
	//m_boxLeft->AddNormal(boxNormals[2]);
	m_boxLeft->AddVertex3dv(box[5]);
	
	m_boxLeft->AddTexCoord(CGrVector(0,0,1));
	m_boxLeft->AddTexCoord(CGrVector(z/3,0,1));
	m_boxLeft->AddTexCoord(CGrVector(z/3,1,1));
	m_boxLeft->AddTexCoord(CGrVector(0,1,1));

	m_boxRight = new CSgPolygon();
	m_boxRight->AddNormal(boxNormals[5]);
	m_boxRight->AddVertex3dv(box[0]);
	//m_boxRight->AddNormal(boxNormals[5]);
	m_boxRight->AddVertex3dv(box[4]);
	//m_boxRight->AddNormal(boxNormals[5]);
	m_boxRight->AddVertex3dv(box[7]);
	//m_boxRight->AddNormal(boxNormals[5]);
	m_boxRight->AddVertex3dv(box[3]);
	

	m_boxRight->AddTexCoord(CGrVector(z/3,0,1));
	m_boxRight->AddTexCoord(CGrVector(z/3,1,1));
	m_boxRight->AddTexCoord(CGrVector(0,1,1));
	m_boxRight->AddTexCoord(CGrVector(0,0,1));


	m_boxFront = new CSgPolygon();
	m_boxFront->AddNormal(boxNormals[0]);
	m_boxFront->AddVertex3dv(box[7]);
	//m_boxFront->AddNormal(boxNormals[0]);
	m_boxFront->AddVertex3dv(box[6]);
	//m_boxFront->AddNormal(boxNormals[0]);
	m_boxFront->AddVertex3dv(box[2]);
	//m_boxFront->AddNormal(boxNormals[0]);
	m_boxFront->AddVertex3dv(box[3]);
		

	m_boxFront->AddTexCoord(CGrVector(1,1,1));
	m_boxFront->AddTexCoord(CGrVector(0,1,1));
	m_boxFront->AddTexCoord(CGrVector(0,0,1));
	m_boxFront->AddTexCoord(CGrVector(1,0,1));


	m_boxBack = new CSgPolygon();
	m_boxBack->AddNormal(boxNormals[3]);
	m_boxBack->AddVertex3dv(box[0]);
	//m_boxBack->AddNormal(boxNormals[3]);
	m_boxBack->AddVertex3dv(box[1]);
	//m_boxBack->AddNormal(boxNormals[3]);
	m_boxBack->AddVertex3dv(box[5]);
	//m_boxBack->AddNormal(boxNormals[3]);
	m_boxBack->AddVertex3dv(box[4]);
	
	m_boxBack->AddTexCoord(CGrVector(0,0,1));
	m_boxBack->AddTexCoord(CGrVector(1,0,1));
	m_boxBack->AddTexCoord(CGrVector(1,1,1));
	m_boxBack->AddTexCoord(CGrVector(0,1,1));



	m_boxTop = new CSgPolygon();
	m_boxTop->AddNormal(boxNormals[1]);
	m_boxTop->AddVertex3dv(box[4]);
	//m_boxTop->AddNormal(boxNormals[1]);
	m_boxTop->AddVertex3dv(box[5]);
	//m_boxTop->AddNormal(boxNormals[1]);
	m_boxTop->AddVertex3dv(box[6]);
	//m_boxTop->AddNormal(boxNormals[1]);
	m_boxTop->AddVertex3dv(box[7]);

	m_boxTop->AddTexCoord(CGrVector(0,1,1));
	m_boxTop->AddTexCoord(CGrVector(0,0,1));
	m_boxTop->AddTexCoord(CGrVector(z/3,0,1));
	m_boxTop->AddTexCoord(CGrVector(z/3,1,1));


	/*if(texture)
	{
		m_boxBottom->SetTexture(texture);
		m_boxBottom->AddTexCoord(CGrVector(1, 1, 0));
		m_boxBottom->AddTexCoord(CGrVector(0, 1, 0));
		m_boxBottom->AddTexCoord(CGrVector(0, 0, 0));
		m_boxBottom->AddTexCoord(CGrVector(1, 0, 0));
		m_boxTop->SetTexture(texture);
		m_boxTop->AddTexCoord(CGrVector(1, 1, 0));
		m_boxTop->AddTexCoord(CGrVector(0, 1, 0));
		m_boxTop->AddTexCoord(CGrVector(0, 0, 0));
		m_boxTop->AddTexCoord(CGrVector(1, 0, 0));
		m_boxLeft->SetTexture(texture);
		m_boxLeft->AddTexCoord(CGrVector(1, 1, 0));
		m_boxLeft->AddTexCoord(CGrVector(0, 1, 0));
		m_boxLeft->AddTexCoord(CGrVector(0, 0, 0));
		m_boxLeft->AddTexCoord(CGrVector(1, 0, 0));
		m_boxRight->SetTexture(texture);
		m_boxRight->AddTexCoord(CGrVector(1, 1, 0));
		m_boxRight->AddTexCoord(CGrVector(0, 1, 0));
		m_boxRight->AddTexCoord(CGrVector(0, 0, 0));
		m_boxRight->AddTexCoord(CGrVector(1, 0, 0));
		m_boxFront->SetTexture(texture);
		m_boxFront->AddTexCoord(CGrVector(1, 1, 0));
		m_boxFront->AddTexCoord(CGrVector(0, 1, 0));
		m_boxFront->AddTexCoord(CGrVector(0, 0, 0));
		m_boxFront->AddTexCoord(CGrVector(1, 0, 0));
		m_boxBack->SetTexture(texture);
		m_boxBack->AddTexCoord(CGrVector(1, 1, 0));
		m_boxBack->AddTexCoord(CGrVector(0, 1, 0));
		m_boxBack->AddTexCoord(CGrVector(0, 0, 0));
		m_boxBack->AddTexCoord(CGrVector(1, 0, 0));
	}*/
	
}


CSgBox::~CSgBox(void)
{
}

void CSgBox::Render()
{
	m_boxTop->Render();
	m_boxBottom->Render();
	m_boxLeft->Render();
	m_boxRight->Render();
	m_boxFront->Render();
	m_boxBack->Render();

}
